/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.system.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.bsl.mam.app.application.AppMainApplication;
import com.foreveross.bsl.mam.app.application.vo.AppMainVo;
import com.foreveross.bsl.system.application.AppCloudLinkService;
import com.foreveross.bsl.system.application.CloudFunctionService;
import com.foreveross.bsl.system.application.vo.CloudFunctionVo;

/**
 * xxx
 *
 * @author Wangyi
 * @version v1.0
 *
 * @date 2013-7-5
 *
 */
@Controller
@RequestMapping("/system/cloud")
public class CloudFuncLinkController extends SystemBaseController {
	
	@Inject
	private CloudFunctionService cloudFunctionApplication;
	
	@Inject
	private AppMainApplication appMainApplication;
	
	@Inject
	private AppCloudLinkService appCloudLinkApplication;

	@RequestMapping()
	public String listAll(Model model){
		List<CloudFunctionVo> list=this.cloudFunctionApplication.findAll();
		Map<String, Integer> counts=appCloudLinkApplication.getLinkedCounts(this.getCurrentUserId());
		List<LinkedCloudFunction> clouds=LinkedCloudFunction.buildWithCloudCounts(list, counts);
		model.addAttribute("clouds", clouds);
		return "cloud/cloud-list";
	}
	
	/**
	 * 展示可关联的应用,即该租户相对指定的云服务还未关联的应用
	 * @param cloudId
	 * @param model
	 * @return
	 */
	@RequestMapping("available-apps/{cloudId}")
	public String showAvailableLinkingApps(@PathVariable("cloudId") String cloudId, Model model){
		List<AppMainVo> apps=this.appMainApplication.findAll();
		String userId=this.getCurrentUserId();
		List<String> linkedAppIds=appCloudLinkApplication.getLinkedAppIds(userId, cloudId);
		List<AppMainVo> availableApps=new ArrayList<AppMainVo>(apps.size());
		boolean nolinked=true;
		for(AppMainVo app : apps){
			nolinked=true;
			for(String appId : linkedAppIds){
				if(app.getId().equals(appId)){
					nolinked=false;
					break;
				}
			}
			if(nolinked){
				availableApps.add(app);
			}
		}
		model.addAttribute("apps", availableApps);
		model.addAttribute("cloudId", cloudId);
		return "cloud/available-apps";
	}
	
	/**
	 * 展示已关联的应用
	 * @param cloudId
	 * @param model
	 * @return
	 */
	@RequestMapping("linked-apps/{cloudId}")
	public String showLinkedApps(@PathVariable("cloudId") String cloudId, Model model){
		List<AppMainVo> apps=this.appMainApplication.findAll();
		String userId=this.getCurrentUserId();
		List<String> linkedAppIds=appCloudLinkApplication.getLinkedAppIds(userId, cloudId);
		List<AppMainVo> linkedApps=new ArrayList<AppMainVo>(apps.size());
		for(AppMainVo app : apps){
			for(String appId : linkedAppIds){
				if(app.getId().equals(appId)){
					linkedApps.add(app);
				}
			}
		}
		CloudFunctionVo cf=this.cloudFunctionApplication.getById(cloudId);
		model.addAttribute("apps", linkedApps);
		model.addAttribute("cloud", cf);
		return "cloud/linked-apps";
	}
	
	@RequestMapping(value="/link", method=RequestMethod.GET)
	public @ResponseBody String link(@RequestParam String appId, @RequestParam String cloudId){
		String userId=this.getCurrentUserId();
		this.appCloudLinkApplication.link(appId, cloudId, userId);
		return "success";
	}


    /**
     * 展示可取消关联的应用,即该租户相对指定的云服务已经关联的应用
     * @param cloudId
     * @param model
     * @return
     */
    @RequestMapping("unlinkable-apps/{cloudId}")
    public String showUnLinkableApps(@PathVariable("cloudId") String cloudId, Model model){
        String userId=this.getCurrentUserId();
        List<String> linkedAppIds=appCloudLinkApplication.getLinkedAppIds(userId, cloudId);
        List<AppMainVo> allApps = appMainApplication.findAll();
        List<AppMainVo> linkedApps = new ArrayList<AppMainVo>();

        for(int i = 0; i < linkedAppIds.size();i++){
            for(int j = 0; j < allApps.size(); j++){
                if(linkedAppIds.get(i).equals(allApps.get(j).getId())){      //已关联
                    linkedApps.add(allApps.get(j));
                }
            }
        }

        model.addAttribute("apps", linkedApps);
        model.addAttribute("cloudId", cloudId);
        return "cloud/unlink-apps";
    }

    @RequestMapping(value="/unlink", method=RequestMethod.GET)
    public @ResponseBody String unlink(@RequestParam String appId, @RequestParam String cloudId){
        appCloudLinkApplication.unlink(appId, cloudId);
        return "success";
    }
}
